﻿import { InventoryService } from '../service/inventoryService';
import { FrontStoreStockService } from '../service/frontStoreStockService';
import { Response, ServiceConfig, route } from '../common';
let lodash = require('lodash');

export class DataSyncController {

    private inventorySvr: InventoryService;
    private frontStoreStockSvr: FrontStoreStockService;
    constructor() {
        this.inventorySvr = ServiceConfig.Current.GetService<InventoryService>("inventoryService");
        this.frontStoreStockSvr = ServiceConfig.Current.GetService<FrontStoreStockService>("frontStoreStockService");
    }

    toString() {
        return "DataSyncController";
    }

    @route({ method: 'get', path: '/api/dataSync/resourceList/:storeId' })
    async getResourceList(ctx, next) {
        let storeId: string = ctx.params.storeId;
        let result = [];
        let r = await this.inventorySvr.GetInventoryResourceUrl(storeId);
        let cr = await this.inventorySvr.GetCategoryResourceUrl(storeId);
        let mr = await this.inventorySvr.GetModifierResourceUrl(storeId);

        if (r && r.length > 0) result = result.concat(r);
        if (cr && cr.length > 0) result = result.concat(cr);
        if (mr && mr.length > 0) result = result.concat(mr);

        ctx.response.body = Response.ok("获取成功", result);

        await next();
    }
    @route({ method: 'get', path: '/api/dataSync/category/:storeId/:id' })
    async getCategoryById(ctx, next) {
        let storeId: string = ctx.params.storeId;
        let categoryId = ctx.params.id;

        let r = await this.inventorySvr.GetCategoryById(categoryId);
        ctx.response.body = Response.ok("获取菜品分类成功", r);

        await next();
    }
    @route({ method: 'get', path: '/api/dataSync/inventory/:storeId/:id' })
    async getInventoryById(ctx, next) {
        let storeId: string = ctx.params.storeId;
        let inventoryId = ctx.params.id;

        let r = await this.inventorySvr.GetInventoryById(inventoryId);
        if (!r.categories) r.categories = [];
        ctx.response.body = Response.ok("获取菜品成功", r);

        await next();
    }
    @route({ method: "get", path: "/api/dataSync/modifier/:storeId/:id" })
    async getModifierById(ctx, next) {
        let storeId = ctx.params.storeId;
        let modifierId = ctx.params.id;

        let r = await this.inventorySvr.GetModifierById(modifierId);
        ctx.response.body = Response.ok("获取成功", r);

        await next();
    }
    @route({ method: "get", path: "/api/dataSync/soldout/:storeId/:id" })
    async getSoldOutById(ctx, next) {
        let storeId = ctx.params.storeId;
        let soldOutId = ctx.params.id;

        let r = await this.inventorySvr.GetSoldOutById(soldOutId);
        ctx.response.body = Response.ok("获取成功", r);
        await next();
    }

    @route({ method: "get", path: "/api/dataSync/soldoutdInventories/:storeId" })
    async getSoldOutInventoriesByStoreId(ctx, next) {
        let storeId = ctx.params.storeId;
        // 获取已经沽清的商品 ID
        let stocks = await this.frontStoreStockSvr.GetStoreSoldOutInventoryIds(storeId);
        let inventoryIds = lodash.map(stocks, 'inventoryId');
        let inventories = [];
        lodash.map(inventoryIds, x => {
            inventories.push({
                id: x,
                isSoldOut: true
            })
            return x;
        });
        ctx.response.body = Response.ok("获取已沽清商品列表成功", inventories);
        await next();
    }
}